home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / asm_msc1.arc / EX53.ASM < prev    next >
Assembly Source File  |  1988-11-20  |  2KB  |  40 lines

  1. TITLE  Maximum & Minimum in an Unordered List (EX53.ASM)
  2.           PAGE      ,132
  3. OUR_CODE  SEGMENT   PARA 'CODE'
  4.       PUBLIC    MINMAX
  5. MINMAX    PROC      FAR
  6.           ASSUME    CS:OUR_CODE
  7.       PUSH        CX
  8.       PUSH        DI             ;Save starting address
  9.       MOV        CX,ES:[DI]         ;Fetch element count
  10.       DEC        CX             ;Get ready for count-1 compares
  11.       PUSH        CX             ;Save this count value
  12.       MOV        BX,ES:[DI+2]     ;To start, 1st el. is minimum
  13.       MOV        AX,BX         ; and maximum
  14. ;
  15. ;  These instructions find the minimum value in the list.
  16. ;
  17.       ADD        DI,4         ;Point to 2nd element
  18.       PUSH        DI             ; and save this pointer
  19. CHKMIN:      CMP        ES:[DI],BX         ;Compare next el. to minimum
  20.       JAE        NOMIN         ;New minimum found?
  21.       MOV        BX,ES:[DI]         ; Yes.  Put it in BX
  22. NOMIN:      ADD        DI,2         ;Point to next element
  23.       LOOP        CHKMIN
  24. ;
  25. ;  These instructions find the maximum value in the list.
  26. ;
  27.       POP        DI             ;Point to 2nd element
  28.       POP        CX             ;Reload comparison counter
  29. CHKMAX:      CMP        ES:[DI],AX         ;Compare next el. to maximum
  30.       JBE        NOMAX         ;New maximum found?
  31.       MOV        AX,ES:[DI]         ; Yes.  Put it in AX
  32. NOMAX:      ADD        DI,2         ;Point to next element
  33.       LOOP        CHKMAX         ;Check entire list
  34.       POP        DI             ;Restore starting address
  35.       POP        CX
  36.       RET                 ; and exit
  37. MINMAX    ENDP
  38. OUR_CODE  ENDS
  39.          END       MINMAX
  40.